package com.ruoyi.orderEat.service.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OssUtil;
import com.ruoyi.common.utils.RedisUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.orderEat.domain.CategoryInfoVO;
import com.ruoyi.orderEat.domain.ShopInfoVO;
import com.ruoyi.orderEat.domain.WxShopVO;
import com.ruoyi.orderEat.service.IShopInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.orderEat.mapper.CategoryInfoMapper;
import com.ruoyi.orderEat.domain.CategoryInfo;
import com.ruoyi.orderEat.service.ICategoryInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import static com.ruoyi.orderEat.service.IShopInfoService.CART_SHOP;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-17
 */
@Service
@Transactional
public class CategoryInfoServiceImpl implements ICategoryInfoService
{
    @Autowired
    private CategoryInfoMapper categoryInfoMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 查询商品分类
     *
     * @param categoryId 商品分类主键
     * @return 商品分类
     */
    @Override
    public CategoryInfo selectCategoryInfoByCategoryId(Long categoryId)
    {
        return categoryInfoMapper.selectCategoryInfoByCategoryId(categoryId);
    }

    /**
     * 查询商品分类列表
     *
     * @param categoryInfo 商品分类
     * @return 商品分类
     */
    @Override
    public List<CategoryInfo> selectCategoryInfoList(CategoryInfo categoryInfo)
    {
        return categoryInfoMapper.selectCategoryInfoList(categoryInfo);
    }

    @Override
    public List<CategoryInfoVO> queryCategoryInfoVODropDown() {
        List<CategoryInfoVO> categoryInfoVOS = categoryInfoMapper.selectCategoryInfoVODropDown();
        return categoryInfoVOS;
    }

    @Override
    public List<WxShopVO> queryWxShopVO() {
        Long userId = SecurityUtils.getUserId();
        List<WxShopVO> wxShopVOS = categoryInfoMapper.selectWxShop();
        for (WxShopVO wxShopVO : wxShopVOS) {
            Integer count = 0;
            List<ShopInfoVO> shopInfoVOList = wxShopVO.getShopInfoVOList();
            for (ShopInfoVO shopInfoVO : shopInfoVOList) {
                Long shopId = shopInfoVO.getShopId();
                String key = CART_SHOP+userId+":"+shopId;
                shopInfoVO.setShopNum((Integer)redisUtil.hGet(key, "shopNum"));
                if (redisUtil.hGet(key, "shopNum")!=null){
                    count+=(Integer)redisUtil.hGet(key, "shopNum");
                }
            }
            wxShopVO.setCount(count);
        }
        return wxShopVOS;
    }

    /**
     * 查询商品分类列表-修改
     *
     * @param categoryInfo 商品分类
     * @return 商品分类
     */
    @Override
    public List<CategoryInfoVO> selectCategoryInfoVoList(CategoryInfo categoryInfo) {
        return categoryInfoMapper.selectCategoryInfoVoList(categoryInfo);
    }

    /**
     * 新增商品分类
     *
     * @param categoryInfo 商品分类
     * @return 结果
     */
    @Override
    public int insertCategoryInfo(CategoryInfo categoryInfo)
    {
        String user = String.valueOf(SecurityUtils.getUserId());
        categoryInfo.setCreateTime(DateUtils.getNowDate());
        categoryInfo.setCreateBy(user);
        return categoryInfoMapper.insertCategoryInfo(categoryInfo);
    }

    /**
     * 修改商品分类
     *
     * @param categoryInfo 商品分类
     * @return 结果
     */
    @Override
    public int updateCategoryInfo(CategoryInfo categoryInfo)
    {
        categoryInfo.setUpdateTime(DateUtils.getNowDate());
        return categoryInfoMapper.updateCategoryInfo(categoryInfo);
    }

    /**
     * 批量删除商品分类
     *
     * @param categoryIds 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryInfoByCategoryIds(Long[] categoryIds)
    {
        List<String> urls = Arrays.stream(categoryIds).map(item -> {
            CategoryInfo categoryInfo = categoryInfoMapper.selectCategoryInfoByCategoryId(item);
            return categoryInfo.getCategoryUrl();
        }).collect(Collectors.toList());
        for (String url : urls) {
            String objectName = url.substring(1);
            OssUtil.deleteOss(objectName);
        }
        return categoryInfoMapper.deleteCategoryInfoByCategoryIds(categoryIds);
    }

    /**
     * 删除商品分类信息
     *
     * @param categoryId 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryInfoByCategoryId(Long categoryId)
    {
        CategoryInfo categoryInfo = categoryInfoMapper.selectCategoryInfoByCategoryId(categoryId);
        String objectName = categoryInfo.getCategoryUrl().substring(1);
        OssUtil.deleteOss(objectName);
        return categoryInfoMapper.deleteCategoryInfoByCategoryId(categoryId);
    }

    @Override
    public String uploadPicture(MultipartFile file) {
        try {
            String upload = FileUploadUtils.uploadToOss(file);
            return upload;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deletePicture(String url) {
        String objectName = url.substring(1);
        OssUtil.deleteOss(objectName);

    }
}
